﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Windows.Interop;
using System.Collections.ObjectModel;
using Pavilion.DesignerTool;
using System.Windows.Controls.Primitives;

namespace Pavilion.DesignerTool
{
    public partial class MainWindow : Window
    {
        private XnaView xnaView;
        private DrawingToolView drawToolView;
        private NodeDesignerToolView nodeDesignerToolView;
        private EnvironmentToolView environmentTool;
        private LoggerWindow loggerWindow;
        private GameContentData ResourceDataManager { get { return (GameContentData)Resources["resourceDataManager"]; } }

        public MainWindow()
        {
            InitializeComponent();

            xnaView = new XnaView();
            xnaView.XnaComponent = new Engine();

            drawToolView = new DrawingToolView();
            nodeDesignerToolView = new NodeDesignerToolView();
            environmentTool = new EnvironmentToolView();

            ResourceDataManager.Load();
            DisplayGame();
        }

        private void DisplayDrawTool()
        {
            contentControl.Content = drawToolView;
            drawToolView.Visibility = Visibility.Visible;

            xnaView.Visibility = Visibility.Hidden;
            nodeDesignerToolView.Visibility = Visibility.Hidden;
            environmentTool.Visibility = Visibility.Hidden;
        }

        private void DisplayDesignerTool()
        {
            contentControl.Content = nodeDesignerToolView;
            nodeDesignerToolView.Visibility = Visibility.Visible;

            drawToolView.Visibility = Visibility.Hidden;
            xnaView.Visibility = Visibility.Hidden;
            environmentTool.Visibility = Visibility.Hidden;
        }

        private void DisplayEnvironmentTool()
        {
            contentControl.Content = environmentTool;
            environmentTool.Visibility = Visibility.Visible;

            drawToolView.Visibility = Visibility.Hidden;
            nodeDesignerToolView.Visibility = Visibility.Hidden;
            xnaView.Visibility = Visibility.Hidden;
        }

        private void DisplayGame()
        {
            contentControl.Content = xnaView;
            xnaView.Visibility = Visibility.Visible;

            drawToolView.Visibility = Visibility.Hidden;
            nodeDesignerToolView.Visibility = Visibility.Hidden;
            environmentTool.Visibility = Visibility.Hidden;
        }

        private void btnDrawingTool_Click(object sender, RoutedEventArgs e) { DisplayDrawTool(); }
        private void btnDesignerTool_Click(object sender, RoutedEventArgs e) { DisplayDesignerTool(); }
        private void btnEnvironmentTool_Click(object sender, RoutedEventArgs e) { DisplayEnvironmentTool(); }
        private void btnGame_Click(object sender, RoutedEventArgs e) { DisplayGame(); }
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //GraphicsDeviceService.CreateGraphicsDeviceService((PresentationSource.FromVisual(this) as HwndSource).Handle);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            GraphicsDeviceService.Instance.Release();

            if (loggerWindow != null)
                loggerWindow.Close();
        }

        private void SaveResourcesButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("About save all modified game data. Proceed?", "Are you sure about this?!", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                ResourceDataManager.SaveResourceItems();
                MessageBox.Show("Modified game data saved!!!");
            }
        }

        private void LoadResource(AnimationResourceData animationResourceData)
        {
            nodeDesignerToolView.LoadResourceItem(animationResourceData);
            drawToolView.LoadResourceItem(animationResourceData);
        }

        private void SaveLayoutsButton_Click(object sender, RoutedEventArgs e)
        {
            nodeDesignerToolView.ViewModel.SaveNodeLayoput();
        }

        private void btnResourceProperties_Click(object sender, RoutedEventArgs e)
        {
            if (btnResourceProperties.Content.ToString() == "5")
            {
                btnResourceProperties.Content = "6";
                resourceDockPanel.Visibility = Visibility.Collapsed;
            }
            else
            {
                btnResourceProperties.Content = "5";
                resourceDockPanel.Visibility = Visibility.Visible;
            }
        }

        private void AddAnimationMenuItem_Click(object sender, RoutedEventArgs e)
        {
            AnimationResourceData animationResourceData = (sender as FrameworkElement).DataContext as AnimationResourceData;
            animationResourceData.AddNewAnimation();
        }

        private void AddResourceButton_Click(object sender, RoutedEventArgs e)
        {
            AnimationResourceData createdAnimationResourceData = ResourceDataManager.CreateNewResource();

            LoadResource(createdAnimationResourceData);
        }

        private void RemoveResourceMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ResourceDataManager.RemoveResource(resourceTreeView.SelectedItem as AnimationResourceData);

            LoadResource(null as AnimationResourceData);
        }

        private void RemoveAnimationMenuItem_Click(object sender, RoutedEventArgs e)
        {
            (resourceTreeView.SelectedItem as AnimationData).Dispose();
        }

        private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            AnimationResourceData animationResourceData = (sender as FrameworkElement).DataContext as AnimationResourceData;

            LoadResource(animationResourceData);
            ResourceDataManager.SelectedResource = animationResourceData;
        }

        private void TextBox_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;

            if (textBox.Focusable)
            {
                textBox.Focus();
                textBox.SelectAll();
            }
        }

        private void LoggerButton_Click(object sender, RoutedEventArgs e)
        {
            if (loggerWindow == null || !loggerWindow.IsLoaded)
            {
                loggerWindow = new LoggerWindow();
                loggerWindow.Show();
            }
            else
            {
                loggerWindow.Activate();
            }
        }
    }
}
